﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Lari.Configuration;
using Lari.Framework.Exceptions;
using Lari.Services.Utility;

namespace Lari.Web.Utility
{
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public class LariController : Controller
    {
/*
        private readonly MasterManager _mgr = new MasterManager();
*/

        #region Adding message

        public void AddInfoMessage(string msg)
        {
            AddMessage(msg, ApplicationConfig.InfoKey);
        }

        //protected void AddInfoMessage(int msgCode)
        //{
        //    AddMessage(XMLMessaage.Message(msgCode), ApplicationConfig.InfoKey);
        //}


        public void AddErrorMessage(string msg)
        {
            AddMessage(msg, ApplicationConfig.ErrorKey);
        }
        //public void AddErrorMessage(int msgCode)
        //{
        //    AddMessage(XMLMessaage.Message(msgCode), ApplicationConfig.ErrorKey);
        //}

        /*
                protected void AddWarningMessage(string msg)
                {
                    AddMessage(msg, ApplicationConfig.WarningKey);
                }
        */
        /*
                protected void AddWarningMessage(int msgCode)
                {
                    AddMessage(XMLMessaage.Message(msgCode), ApplicationConfig.WarningKey);
                }
        */
        private void AddMessage(string msg, string key)
        {
            if (TempData[key] == null)
            {
                var temp = new List<string> { msg };
                TempData[key] = temp;
            }
            else
            {
                IList<string> temp = TempData[key] as List<string>;
                if (temp != null)
                {
                    temp.Add(msg);
                    TempData[key] = temp;
                }
            }

        }

        #endregion

        protected void ProcessException(string errorMessage, Exception ex, string moduleName, IDictionary<string, object> extentedProperties = null)
        {
            if (ex is LariException)
            {
                AddErrorMessage(ex.Message);

                ErrorHandler.ProcessException(errorMessage, ex, moduleName, extentedProperties);

            }
            //else if ((ex is FaultException<LariFault> || ex.GetBaseException() is FaultException<LariFault>))
            //{
            //    var f = ex as FaultException<LariFault>;
            //    if (f != null) AddErrorMessage(f.Detail.CustomMessage);
            //    ErrorHandler.ProcessException(errorCode, ex, moduleName, extentedProperties);
            //}
            else if (ex is AggregateException)
            {
                var aggex = ex as AggregateException;
                foreach (var err in aggex.InnerExceptions)
                {
                    ErrorHandler.ProcessException(errorMessage, err, moduleName, extentedProperties);
                }
                AddErrorMessage(errorMessage);
            }
            else
            {
                AddErrorMessage(errorMessage);
                ErrorHandler.ProcessException(errorMessage, ex, moduleName, extentedProperties);
            }



        }

        protected ActionResult NoDataFound { get { return View("NoData"); } }
    }
}
